package com.nondo.rdp.auth.security;

import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.security.model.UserContext;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * com.seelyn.superp.core.security
 *
 * @author linfeng
 * @since 2017/7/30
 */
@Component
public class SpringSecurityAuditorAware implements AuditorAware<User> {

    @Override
    public User getCurrentAuditor() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }

        return ((UserContext) authentication.getPrincipal()).getUser();

    }
}
